"""
This script is used to run all the test of openttd performance
"""
import Process,os,time,  sys, math,Analysis, Analysis2,DB
from threading import Timer
from Configuration import Configuration
from Performance import Performance
import test1c_server

def expVarChange(expName, var):
    if(expName == "1a1"):
        Configuration.set("difficulty", "max_no_competitors", var)
        Configuration.setAI(var)
    if expName == "1a2":
        Configuration.set("game_creation", "map_x", var)
        Configuration.set("game_creation", "map_y", var)
    if expName == "1a3":
        Configuration.set("difficulty", "number_towns", var)
        Configuration.set("difficulty", "number_industries", var)
    if expName == "1a4":
        Configuration.set("difficulty", "max_no_competitors", var)
        Configuration.setAI(16)
        Configuration.set("game_creation", "map_x", 11)
        Configuration.set("game_creation", "map_y", 11)
    return True

def expEcho(expName, turn, var):
    output = "turn %d"%turn
    if expName == "1a1" :
        output = output + " AIs %d"%var
    if expName == "1a2":
        output = output + "MapSize %d"%math.pow(2,var)
    if expName == "1a3":
        output = output + "number_towns = number_industries = %d"%var
    if expName == "1a4":
        output = output + "max abillity test"
    if expName == "1c":
        output = output + " server"
    return output

def expEcho2(expName, turn, var):
    output = ""
    if expName == "1a1":
        output = "NumOfAIs " + "%d"%i
    if expName == "1a2":
        output = "MapSize %d"%math.pow(2,i)
    if expName == "1a3":
        output = "number_towns = number_industries = %d"%var
    if expName == "1a4":
        output = "AI = 16, MapSize = 2048 * 2048 "
    if expName == "1c":
        output = " server"
    output = output + " Process.maxYear = %d"%Process.maxYear
    return output
minVars = { "1a1":1, "1a2":6, "1a3":1, "1a4":1, "1c":1}
#maxVars = { "1a1":8, "1a2":11, "1a3":4}
maxVars = { "1a1":16, "1a2":11, "1a3":4, "1a4":1, "1c":4}
#expNames = ["1a1", "1a2", "1a3", "1c"]
#expNames = ["1a1", "1a2", "1a3"]
#1a4 is the max allowed test
expNames = ["1c"]

totalTurns = 1
Process.maxYear = 1


path = os.path.join("..","test_result")
if os.path.exists(path) == False:
    os.mkdir(path)
f = open(os.path.join(path, "networkName"),"a")
measurement = Performance()
measurement.flag = 1
measurement.start()
time.sleep(3)
print >>f, measurement.networkFileName
f.close()
log = open(os.path.join(path, "log"),"a")
Configuration.increaseId()
expId = Configuration.getId()
Analysis.updateSkipList(Process.maxYear)
for expName in expNames:
    print >> log, expName
    path2 = os.path.join(path, expName)
    if os.path.exists(path2) == False:
        os.mkdir(path2)
    for turn in xrange(1, totalTurns + 1/1):
        filename = os.path.join(path2 ,"test"+ expName + "_result_"+"%d"%turn+".txt")
        file = open(filename,"w")
        print >> file, maxVars[expName] - minVars[expName] + 1
        print >>log,"turn%d"%turn, maxVars[expName] - minVars[expName] + 1
        for i in xrange(minVars[expName],maxVars[expName] + 1/1):
            Configuration.initExp(expName)
            expVarChange(expName, i)
            starttime = time.time()
            print >> file, "-----------" + time.asctime() + "----------- %d"%expId + "_" + expName + "\n%f"%starttime 
            print >>file , expEcho2(expName, turn, i), " %d"%Configuration.getId()
            print >> log, "-----------" + time.asctime() + "----------- %d"%expId + "_" + expName + "\n%f"%starttime
            print >>log , expEcho2(expName, turn, i)
            print expEcho(expName, turn, i)
            
            if expName == "1c":
                test1c_server.server(i, expId, turn)
            else:
                if sys.platform == "win32":
                    os.system("cygwin.bat") 
                else :
                    "if you want to start a game with a visual end use -g"
                    os.system("~/software/openttd-1.0.4/bin/openttd -D&")
            Process.killOpenTTD(file)
            
            endtime = time.time()
            print >> file,"%f"%endtime + "\n-----------" + time.asctime() + "-----------\n"
            print >> log,"%f"%endtime + "\n-----------" + time.asctime() + "-----------\n"
            file.flush()
            filename = "%d"%expId + "_" + expName + "_%d"%turn + "_%d"%i
            if expName == "1c":
                DB.type = 2
                test1c_server.copyFile(expId, i)
            DB.insert(expId, expName, turn, i, starttime, endtime, -1.0, Process.maxYear, filename)
            measurement.save()
    try:
        Analysis.updateSkipList(Process.maxYear)
        Analysis.staticsCPUandMem(expId, expName, 1, totalTurns, minVars[expName], maxVars[expName], Process.maxYear)
        Analysis.drawCPUGraph(expId, expName, 1, turn, minVars[expName], maxVars[expName], Process.maxYear)
        Analysis.network(expId, "1c", 1, totalTurns, minVars["1c"], maxVars["1c"], Process.maxYear, True)
    except :
        pass
measurement.flag = 0


#for expName in expNames:
#    Analysis.staticsCPUandMem(expId, expName, turn)
##    Analysis2.plot(expName)
#    Analysis2.boxplot(expName, 1)
#    Analysis2.boxplot(expName, 2)
#    Analysis.generateNetworkReport(measurement.networkFileName)
#    Analysis.AnalysisNetwork(expName, totalTurns)
#    Analysis2.plotNetwork(expName)
